package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Shop;
import com.gaohe.service.ShopService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam("page") Integer page,
                  @RequestParam("pageSize") Integer pageSize,
                  @RequestParam(value = "name", required = false) String name) {
        PageInfo<Shop> shopPage = shopService.pageList(page,pageSize,name);
        return R.success(shopPage);
    }


    /**
     * 新增
     *
     * @param shop
     * @return
     */
    @PostMapping
    public R add(@RequestBody Shop shop) {
        //设置活动开始时间  当前时间
        boolean save = shopService.save(shop);
        return save ? R.success("新增成功") : R.error("新增失败");
    }

    /**
     * 根据 id 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.success(shopService.getById(id));
    }

    /**
     * 修改
     *
     * @param shop
     * @return
     */
    @PutMapping
    public R updateById(@RequestBody Shop shop) {
        return shopService.updateById(shop) ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 根据 id 删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById(@RequestParam Long id) {
        return shopService.removeById(id) ? R.success("删除成功") : R.error("删除失败");
    }
    /*------------------小程序端---------------*/

    /**
     * 根据 类型 id 查看商品
     * @param categoryId
     * @return
     */
    @GetMapping("/getShopByCategoryId/{id}")
    public R getShopByCategoryId(@PathVariable("id") Long categoryId){
        LambdaQueryWrapper<Shop> qw = new LambdaQueryWrapper<>();
        qw.eq(Shop::getCategoryId,categoryId);
        return R.success(shopService.list(qw));
    }
}
